package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class BibliotecaApp {

    private static final String CODE_PROMPT = "item code";
    private final Menu menu;
    private final Library library;

    public BibliotecaApp() {
        List<LibraryItem> preLoadedItems = new ArrayList<>();
        preLoadedItems.add(new Book("P01", "TDD: By Example", "Kent Beck"));
        preLoadedItems.add(new Book("P02", "Practical Object Oriented Design in Ruby", "Sandi Metz"));
        preLoadedItems.add(new Book("F01", "The Complete Stories, Vol 1", "Isaac Asimov"));
        preLoadedItems.add(new Movie("M01", "Pulp Fiction", 1994, "Quentin Tarantino", 9));
        preLoadedItems.add(new Movie("M02", "Inception", 2010, "Christopher Nolan", 8));
        preLoadedItems.add(new Movie("M03", "Her", 2013, "Spike Jonze", 0));

        List<User> preLoadedUsers = new ArrayList<>();
        preLoadedUsers.add(new User("111-1111", "secret", "Jonathan Reyes", "j@jreyes.org", "3331234"));
        preLoadedUsers.add(new User("222-2222", "secret", "Juan Perez", "j@perez.me", "5551234"));

        library = new Library(preLoadedItems, preLoadedUsers);
        menu = new Menu();
    }

    public void run() throws ExitException {
        String option;
        String[] additionalInputs;
        String[] arguments;
        Scanner in = new Scanner(System.in);

        menu.registerOption("U", "User Login", new UserLoginCommand(library), "library number", "password");
        menu.registerOption("O", "User Logout", new UserLogoutCommand(library));
        menu.registerOption("D", "Display User Info", new DisplayUserInfoCommand(library));
        menu.registerOption("B", "List Books", new ListBooksCommand(library));
        menu.registerOption("M", "List Movies", new ListMoviesCommand(library));
        menu.registerOption("C", "Checkout Item", new CheckoutItemCommand(library), CODE_PROMPT);
        menu.registerOption("R", "Return Item", new ReturnItemCommand(library), CODE_PROMPT);
        menu.registerOption("Q", "Quit", new QuitCommand());
        menu.makeAuthOnly("O");
        menu.makeAuthOnly("D");
        menu.makeAuthOnly("C");
        menu.makeAuthOnly("R");
        menu.makeNoAuthOnly("U");

        out(menu.welcomeUser());

        while (true) {
            menu.setAuthenticated(library.isAuthenticated());
            out(menu.printMenu());
            option = menu.parseOption(in.next());

            if (menu.getOption(option) == null) {
                out("Select a valid option!\n");
                continue;
            }

            additionalInputs = menu.chooseOption(option);
            arguments = null;

            if (additionalInputs != null) {
                arguments = new String[additionalInputs.length];

                for (int i = 0; i < additionalInputs.length; i++) {
                    out(String.format("Enter %s: ", additionalInputs[i]));
                    arguments[i] = in.next().trim();
                }
            }

            out(menu.callOption(option, arguments));
        }
    }

    private void out(String output) {
        System.out.print(output);
    }

    public static void main(String[] args) {
        BibliotecaApp bibliotecaApp = new BibliotecaApp();
        try {
            bibliotecaApp.run();
        } catch (ExitException e) {
            System.exit(0);
        }
    }
}